﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Shapes;

namespace Halcon二次开发.Tools
{
    internal class HelpJsons
    {
        /// <summary>
        /// Json文件写入，可以自己选择路径
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="path"></param>
        public static void Write<T>(List<T> list, string path)
        {
            using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                DataContractJsonSerializer data = new DataContractJsonSerializer(typeof(List<T>));

                data.WriteObject(file, list);
            }
        }
        /// <summary>
        /// 写字典类
        /// </summary>
        /// <param name="list"></param>
        /// <param name="path"></param>
        public static void Write(Dictionary<string, string> list, string path)
        {
            using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                DataContractJsonSerializer data = new DataContractJsonSerializer(typeof(Dictionary<string, string>));

                data.WriteObject(file, list);
            }
        }
        /// <summary>
        /// 写一个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="path"></param>
        public static void Write<T>(T list, string path)
        {

            using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                DataContractJsonSerializer data = new DataContractJsonSerializer(typeof(T));

                data.WriteObject(file, list);
            }
        }
        /// <summary>
        /// 读取Json集合返回一个List集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">路径</param>
        /// <returns></returns>
        public static List<T> Read<T>(string path)
        {
            //判断有没有此文件
            if (!File.Exists(path))
            {
                return null;
            }
            using (FileStream file = new FileStream(path, FileMode.OpenOrCreate))
            {
                DataContractJsonSerializer data = new DataContractJsonSerializer(typeof(List<T>));
                file.Seek(0, SeekOrigin.Begin);
                try
                {
                    return (List<T>)data.ReadObject(file);
                }
                catch { return null; }
            }
        }
        /// <summary>
        /// 序列化字典类
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Dictionary<string, string> Read(string path)
        {
            //判断有没有此文件
            if (!File.Exists(path))
            {
                return null;
            }
            using (FileStream file = new FileStream(path, FileMode.OpenOrCreate))
            {
                DataContractJsonSerializer data = new DataContractJsonSerializer(typeof(Dictionary<string, string>));
                file.Seek(0, SeekOrigin.Begin);
                try
                {
                    return (Dictionary<string, string>)data.ReadObject(file);
                }
                catch { return null; }
            }
        }
        /// <summary>
        /// 读取属性
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static T ReadObject<T>(string path)
        {
            //判断有没有此文件
            if (!File.Exists(path))
            {
                return default(T);
            }
            using (FileStream file = new FileStream(path, FileMode.OpenOrCreate))
            {
                DataContractJsonSerializer data = new DataContractJsonSerializer(typeof(T));
                file.Seek(0, SeekOrigin.Begin);
                try
                {
                    return (T)data.ReadObject(file);
                }
                catch { return default(T); }
            }
        }
        public static Dictionary<string, int> DeserializeKeyValuePairs(string filePath)
        {
            if (!File.Exists(filePath))
            {
                var emptyDict = new Dictionary<string, int>(); // 如果文件不存在，返回空集合
                SerializeKeyValuePairs(emptyDict, filePath);  // 创建文件并初始化为空集合
                return emptyDict;
            }

            using (var stream = new FileStream(filePath, FileMode.Open))
            {
                var formatter = new BinaryFormatter();
                return (Dictionary<string, int>)formatter.Deserialize(stream); // 反序列化键值对集合
            }
        }
        // 序列化键值对集合
        public static void SerializeKeyValuePairs(Dictionary<string, int> keyValuePairs,string filePath)
        {
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, keyValuePairs); // 将集合序列化到文件
            }
        }
    }
}
